import { getDictionaryDataSelector } from '@/api/systemData/dictionary'
import request from '@/utils/request'
import ExportBox from './ExportBox'
import CustomForm from './Form/index.vue'

export default {
  components: { CustomForm, ExportBox },
  data() {
    return {
      showAll: false,
      query: {
        time: undefined,
        method: undefined,
        type: undefined
      },
      list: [],
      listLoading: true,
      multipleSelection: [],
      total: 0,
      listQuery: {
        currentPage: 1,
        pageSize: 20,
        sort: 'desc',
        sidx: ''
      },
      formVisible: false,
      exportBoxVisible: false,
      columnList: [{ prop: 'id', label: '订单编号' }, { prop: 'name', label: '客户名称' }, { prop: 'campusName', label: '校区名称' }, { prop: 'status', label: '款项类型' }, { prop: 'brand', label: '收款品牌' }, { prop: 'type', label: '退款类型' }, { prop: 'method', label: '退款方式' }, { prop: 'amount', label: '退款金额' }, { prop: 'time', label: '退款时间' }, { prop: 'reason', label: '退款原因' }],
      /* methodOptions: [{ 'fullName': '银行转账', 'id': '1' }, { 'fullName': '微信转账', 'id': '2' }, {
        'fullName': '支付宝转账',
        'id': '3'
      }, { 'fullName': '现金', 'id': '4' }],
      typeOptions: [{ 'fullName': '全额退款', 'id': '1' }, { 'fullName': '部分退款', 'id': '2' }],*/
      methodOptions: [{ fullName: '银行转账', id: '1' }, { fullName: '微信转账', id: '2' }, { fullName: '支付宝转账', id: '3' }, { fullName: '现金', id: '4' }],
      typeOptions: [{ fullName: '全额退款', id: '1' }, { fullName: '部分退款', id: '2' }],
      statusOptions: [{ fullName: '定金', id: '1' }, { fullName: '加盟费', id: '2' }, { fullName: '续费', id: '3' }, { fullName: '保证金', id: '4' }],
      brandOptions: []
    }
  },
  computed: {},
  created() {
    this.initData()
    this.getBrandOptions()
  },
  methods: {
    getBrandOptions() {
      getDictionaryDataSelector('Brand').then(res => {
        this.brandOptions = res.data.list
      })
    },
    download(data) {
      let query = { ...data, ...this.listQuery, ...this.query }
      request({
        url: `api/finance/refunds/Actions/Export`,
        method: 'GET',
        data: query
      }).then(res => {
        if (!res.data.url) return
        window.open(this.define.comUrl + res.data.url)
        this.$refs.ExportBox.visible = false
        this.exportBoxVisible = false
      })
    },
    exportData() {
      this.exportBoxVisible = true
      this.$nextTick(() => {
        this.$refs.ExportBox.init(this.columnList)
      })
    },
    initData() {
      this.listLoading = true
      let _query = {
        ...this.listQuery,
        ...this.query
      }
      let query = {}
      for (let key in _query) {
        if (Array.isArray(_query[key])) {
          query[key] = _query[key].join()
        } else {
          query[key] = _query[key]
        }
      }
      request({
        url: `/api/finance/refunds`,
        method: 'GET',
        data: query
      }).then(res => {
        this.list = res.data.list
        this.total = res.data.pagination.total
        this.listLoading = false
      })
    },
    handleDel(id) {
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        type: 'warning'
      })
        .then(() => {
          request({
            url: `/api/finance/refunds/${id}`,
            method: 'DELETE'
          }).then(res => {
            this.$message({
              type: 'success',
              message: res.msg,
              onClose: () => {
                this.initData()
              }
            })
          })
        })
        .catch(() => {})
    },
    addOrUpdateHandle(id, isDetail) {
      this.formVisible = true
      this.$nextTick(() => {
        this.$refs.CustomForm.init(id, isDetail)
      })
    },
    search() {
      this.listQuery = {
        currentPage: 1,
        pageSize: 20,
        sort: 'desc',
        sidx: 'time'
      }
      this.initData()
    },
    refresh(isrRefresh) {
      this.formVisible = false
      if (isrRefresh) this.reset()
    },
    reset() {
      for (let key in this.query) {
        this.query[key] = undefined
      }
      this.listQuery = {
        currentPage: 1,
        pageSize: 20,
        sort: 'desc',
        sidx: 'time'
      }
      this.initData()
    }
  }
}
